package tools;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :chensi
 */
public class RedisTool {

    /**
     * Scan keys, the result may contain duplicates
     * @param jedis
     * @param params
     * @return keys of params pattern in jedis
     */
    public static List<String> scanKeys(Jedis jedis, ScanParams params) {
        ArrayList<String> keys = new ArrayList<>();
        String cursor = "0";
        do {
            ScanResult<String> scan = jedis.scan(cursor, params);
            keys.addAll(scan.getResult());
            cursor = scan.getStringCursor();
        } while (!cursor.equals("0"));
        return keys;
    }
}
